﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using micsun.Logic;

namespace tmsWeb.Sys
{
    public partial class SysUsers : micsun.Web.SecurityPage
    {
        private DataTable _SchemaTable;
        private LogicArgs args = new LogicArgs();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string inkeyID = string.Empty;
                if (Request.Params["USRID"] != null && Request.Params["USRID"] != string.Empty)
                    inkeyID = Request.Params["USRID"].Split('|')[0];

                int inposition = 0;
                try
                {
                    inposition = int.Parse(Request.Params["Position"]);
                }
                catch
                {
                    inposition = -1;
                }
                if (inkeyID != null && inkeyID != string.Empty)
                {
                    DataGrid2.AddFindField("USERID", inkeyID);
                }
                else
                    DataGrid2.RecordPosition = inposition;

                DataGrid2.SelectList = "USR.USERID,USR.USERNAME,GRP.GRPID,GRP.GRPDESC";
                DataGrid2.TableList = "ISMBUR USR,ISMBTG BTG,ISMGRP GRP";
                DataGrid2.WhereList = "USR.USERID=BTG.USERID and BTG.GRPID=GRP.GRPID";
                DataGrid2.KeyIDName = "USRID";
                DataGrid2.KeyIDPosition = new int[] { 1 };
                DataGrid2.BindData(BindDataType.搜索位置);
                _SchemaTable = DataGrid2.SchemaTable;
                if (DataGrid2.Items.Count == 0)
                {
                    btnDel.Enabled = btnEdt.Enabled = btnQry.Enabled = false;
                }
                initSearchBar();
            }
            else
            {
                if (Request.Params[DataGrid2.KeyIDName] != null)
                {
                    string[] keyIds = Request.Params[DataGrid2.KeyIDName].Split('|');
                    args.Parameters.Add("USRID", keyIds[0]);
                    args.Parameters.Add("Position", keyIds[1]);
                    DataGrid2.KeyIDValue = Request.Params[DataGrid2.KeyIDName];
                }
            }
        }

        void initSearchBar()
        {
            if (_SchemaTable == null) return;
            //_SchemaTable.Rows[0].Delete();
            _SchemaTable.AcceptChanges();
            _SchemaTable.Columns.Add("locdesc");

            for (int i = 0; i < DataGrid2.Columns.Count; i++)
            {
                string opField = string.Empty;
                try
                {
                    if (((BoundColumn)DataGrid2.Columns[i]).Visible == false)
                    {
                        continue;
                    }
                    opField = ((BoundColumn)DataGrid2.Columns[i]).DataField;
                }
                catch
                {
                    continue;
                }
                DataRow[] foundRows = _SchemaTable.Select("ColumnName='" + opField + "'");
                ListItem li = new ListItem(DataGrid2.Columns[i].HeaderText, foundRows[0][0].ToString() + "|" + foundRows[0][11].ToString());
                SelectList.Items.Add(li);
            }
            txtContent.Attributes.Add("onkeydown", "if(event.keyCode==13){SelectBtn.click();return false;}");
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Transfer.Redirect("SYS/SysUsersMtn.aspx?Action=New&USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
        }

        protected void btnEdt_Click(object sender, EventArgs e)
        {
            Transfer.Redirect("SYS/SysUsersMtn.aspx?Action=Edt&USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
        }

        protected void btnQry_Click(object sender, EventArgs e)
        {
            Transfer.Redirect("SYS/SysUsersMtn.aspx?Action=Qry&USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            Transfer.Redirect("SYS/SysUsersMtn.aspx?Action=del&USRID=" + args.Parameters["USRID"] + "&Position=" + args.Parameters["Position"]);
        }

        protected void SelectBtn_Click(object sender, EventArgs e)
        {
            string[] SearchFields = Request.Params[SelectList.UniqueID].Split('|');
            string searchField = SearchFields[0];
            string datatype = SearchFields[1];
            string oprater = Request.Params[SelectLogic.UniqueID];
            string searchvalue = Request.Params[txtContent.UniqueID];
            int stype = 1;
            if (Request.Params[TwoCbx.UniqueID] != null) stype = 2;
            string messagse = DataGrid2.AddSearchCond(searchField, oprater, searchvalue, datatype, (SearchType)stype);
            if (messagse != null && messagse != string.Empty)
                AddMessage(messagse);
            DataGrid2.BindData(BindDataType.分页);
        }

    }
}
